package com.example.shoppingdemo.nslcommodity;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.shoppingdemo.MainActivity;
import com.example.shoppingdemo.R;
import com.example.shoppingdemo.nslcommodity.myview.RoundRectImageView;

import java.util.List;

/**
 * 商品列表的适配器
 */
public class CommodityAdapter extends RecyclerView.Adapter<CommodityAdapter.CommodityHolder>{
    private static final String TAG = MainActivity.class.getSimpleName();
    private List<CommodityModel>commodityModels;
    private Context mContext;
    private int type;
    private onCommodityParticulars commodityParticulars;

    public void setCommodityParticulars(onCommodityParticulars commodityParticulars) {
        this.commodityParticulars = commodityParticulars;
    }

    public CommodityAdapter(Context mContext) {
        this.mContext = mContext;
    }
    public void setCommodityModels(List<CommodityModel> commodityModels) {
        this.commodityModels = commodityModels;
    }
    //点击切换布局的时候通过这个方法设置type
    public void setType(int type) {
        this.type = type;
    }
    @Override
    //用来获取当前项Item是哪种类型的布局
    public int getItemViewType(int position) {
        return type;
    }
    @NonNull
    @Override
    public CommodityHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View baseView;
        if (viewType==0){
            baseView = LayoutInflater.from(parent.getContext()).inflate(R.layout.commodity_list_item, parent, false);
            CommodityHolder holder=new CommodityHolder(baseView);
            return holder;
        }else {
            baseView = LayoutInflater.from(parent.getContext()).inflate(R.layout.commodity_list_item1, parent, false);
          CommodityHolder holder=new CommodityAdapter.CommodityHolder(baseView);
            return holder;
        }


    }

    @Override
    public void onBindViewHolder(@NonNull CommodityAdapter.CommodityHolder holder, int position) {
        Uri uri=Uri.parse(commodityModels.get(position).getIcon());
        Glide.with(mContext).load(uri).into(holder.icon);
        holder.money.setText("￥"+commodityModels.get(position).getMoney());
        holder.title.setText(commodityModels.get(position).getTitle());
        holder.shop.setText(commodityModels.get(position).getShop());
    }


    @Override
    public int getItemCount() {
        return null==commodityModels?0:commodityModels.size();
    }

    class CommodityHolder extends RecyclerView.ViewHolder{
        RoundRectImageView icon;
        TextView title;
        TextView money;
        TextView shop;
        public CommodityHolder(@NonNull View itemView) {
            super(itemView);
            icon= itemView.findViewById(R.id.image_item_commodity);
            title=itemView.findViewById(R.id.text_item_title);
            money=itemView.findViewById(R.id.text_item_money);
            shop=itemView.findViewById(R.id.text_item_shop);
            /**
             * 给item设置监听器
             */
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    commodityParticulars.onCommodityResult(CommodityHolder.this.getAdapterPosition());
                }
            });
        }
    }
    public interface onCommodityParticulars{
        void onCommodityResult(int position);
    }
}
